package tech.luoyu.ki.generator;

import java.sql.Types;
import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.TopLevelClass;

public class JSR303Plugin extends PluginAdapter {

	@Override
	public boolean validate(List<String> warnings) {
		return true;
	}

	@Override
	public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
			IntrospectedTable introspectedTable, ModelClassType modelClassType) {
		if (true == introspectedColumn.isStringColumn()) {
			if (false == introspectedColumn.isNullable()) {
				topLevelClass.addImportedType("org.hibernate.validator.constraints.NotBlank");
				field.addAnnotation("@NotBlank");
			}
			topLevelClass.addImportedType("javax.validation.constraints.Size");
			field.addAnnotation(
					"@Size(min = 0, max = " + introspectedColumn.getLength() + " , message = \"长度必须在{min}和{max}之间\")");
		}

		if (introspectedColumn.getJdbcType() == Types.INTEGER) {
			if (false == introspectedColumn.isNullable()) {
				topLevelClass.addImportedType("javax.validation.constraints.NotNull");
				field.addAnnotation("@NotNull");
			}
			topLevelClass.addImportedType("javax.validation.constraints.Max");
			field.addAnnotation("@Max(value=2147483647,message=\"最大值不能高于{value}\")");
			topLevelClass.addImportedType("javax.validation.constraints.Min");
			field.addAnnotation("@Min(value=-2147483648,message=\"最小值不能低于{value}\")");
		}

		return super.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
	}

}
